#!/bin/sh

source /etc/profile
if [ $# -lt 1 ]; then
    echo "[ERROR] usage: $0 <service-name> <args>"
    exit 2
fi
process=$1
jarName="${process}"
if [ ! -f "$jarName" ]; then
  jarName="${process}-1.0.jar"
  if [ ! -f "$jarName" ]; then
    echo "[ERROR] [$jarName] not exists."
	exit -1
  fi
fi

p=$(ps -ef | grep "[^-]\b${jarName}\b" | grep 'java' | grep -v grep | awk '{print $2}')
if [ -z "$p" ]; then
  echo "Starting $jarName..."
  nohup @dist.java.exec@ -jar @dist.jvm.options@ $2 ${jarName} 1>logs/$process.out 2>&1 &
else
  echo "[:)] [$process] server is already running"
  exit 1
fi
exit 0
